﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

using Kiiro.Common.Utility;
using Kiiro.Model.Utility;

namespace Kiiro.Model.TimerJobs
{
    class DiscussionEmailTimerJob : SPJobDefinition
    {
        public DiscussionEmailTimerJob ( ) : base ( ) { }

        public DiscussionEmailTimerJob ( string jobName, SPService service, SPServer server, SPJobLockType targetType )
            : base ( jobName, service, server, targetType ) { }

        public DiscussionEmailTimerJob ( string jobName, SPWebApplication webApplication )
            : base ( jobName, webApplication, null, SPJobLockType.ContentDatabase )
        {
            this.Title = jobName;
        }

        public override void Execute ( Guid targetInstanceId )
        {
            string guid = Title.Replace ( Constants.SPSiteDefinition.TimerJobs.DiscussionsJobName, "" );

            if ( !string.IsNullOrEmpty ( guid ) )
            {
                Guid siteCollectionId = new Guid ( guid ); ;

                using ( SPSite site = new SPSite ( siteCollectionId ) )
                {
                    foreach ( SPWeb web in site.AllWebs )
                    {
                        if ( SPHelpers.IsPortal ( web ) )
                        {
                            try
                            {
                                string serverRelativeUrl = web.ServerRelativeUrl.Length > 1 ? web.ServerRelativeUrl : "";
                                EmailModel.Instance.CheckPop3Account ( siteCollectionId, serverRelativeUrl );
                            }
                            catch ( Exception ex )
                            {
                                ExceptionManagement.ExceptionManager.HandleException ( ex, false );
                            }
                        }
                    }
                }
            }

            base.Execute ( targetInstanceId );
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/TimerJobs/IncomingDiscussionEmailJob.cs $
 * 
 * 6     26/11/09 10:02a Camerons
 * 
 * 5     30/10/09 12:54p Camerons
 * 
 * 4     4/09/09 3:18p Camerons
 * 
 * 1     4/09/09 10:06a Camerons
 * 
 * 3     17/08/09 10:29a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/